package com.lago.homework2.exercise2;
/*
编程实现控制台版并支持两人对战的五子棋游戏
 */
import java.util.Scanner;
public class Gobang {

    Scanner sc = new Scanner(System.in);
    private char[][] chessboard = new char[16][16];
    private boolean flag = true;                                             //判断是否胜利

    //创建棋盘，利用15x15的二维数组
    public void Chessboard() {

        for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[i].length; j++) {
                if (i == 0 & j == 0) {
                    chessboard[i][j] = (char) 32;
                } else if (i == 0 && j <= 10) {
                    chessboard[i][j] = (char) (47 + j);                      //实现第一行的数字部分
                } else if (i == 0 && j <= 16) {
                    chessboard[i][j] = (char) (j + 86);                      //实现第一行的字母部分
                } else if (j == 0 && i <= 10) {
                    chessboard[i][j] = (char) (47 + i);                      //实现第一列的数字部分
                } else if (j == 0 && i <= 16) {
                    chessboard[i][j] = (char) (i + 86);                      //实现第一列的字母部分
                } else {
                    chessboard[i][j] = (char) 43;                            //实现其余位置的"+"
                }
            }
            System.out.println();
        }
    }

    //提供show方法打印棋盘
    public void show() {
        System.out.println("现在棋盘是下面这样的：");
        for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[i].length; j++) {
                System.out.print(chessboard[i][j] + "\t");
            }
            System.out.println();
        }
    }

    //规定下棋方法
    public void playGo() {

        Scanner sc = new Scanner(System.in);
        boolean swap = true;                    //规定swap是true时黑棋落子

        int row, col;                            //记忆棋子的位置

        while (flag) {
            //swap为ture时，黑方棋手下棋
            if (swap) {
                System.out.println("下面由黑方下棋！");
                System.out.println("请输入黑棋下落的坐标，横竖坐标只限一位，如（0 0）：");
                row = sc.next().charAt(0);                                                  //此处的charAt不太明白是什么原理，但是发现它能实现在输入的字符串取第一位字符，并将字符型转换为整数型
                col = sc.next().charAt(0);

                //下面分成四个不同的部分进行字符向数字的转换
                //第一部分：横纵坐标都是数字的时候
                if (row <= 57 && row >= 48 && col <= 57 && col >= 48) {
                    if (chessboard[row - 47][col - 47] == (char) 43) {                    //坐标的位置转换为对应的二位数组的位置
                        chessboard[row - 47][col - 47] = (char) 38;                      //判断此处是否有棋子存在，如果此处是+号，表示此处没有棋子，否则提示该位置已经有棋子
                        this.judge();                                                       //每次下完棋都要判断是否有一方赢得对弈
                        this.show();                                                        //每次下完棋都要展示当前棋盘
                    }
                    else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");               //提示重新选择下棋位置并且结束当前循环，开启下一轮循环，但是依然是该方选手下棋
                        continue;
                    }
                }
                //第二部分：横坐标为字母，纵坐标为数字的时候
                else if(row <= 102 && row >= 97 && col <= 57 && col >= 48) {
                    if (chessboard[row - 86][col - 47] == (char) 43) {
                        chessboard[row - 86][col - 47] = (char) 38;
                        this.judge();
                        this.show();
                    }
                    else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }

                }
                //第三部分：横坐标为数字，纵坐标为字母的时候
                else if(row <= 57 && row >= 48 && col <= 102 && col >= 97){
                    if (chessboard[row - 47][col - 86] == (char) 43) {
                        chessboard[row - 47][col - 86] = (char) 38;
                        this.judge();
                        this.show();
                    } else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }

                }
                //第四部分：横纵坐标都为字母的时候
                else if(row <= 102 && row >= 97 && col <= 102 && col >= 97){
                    if (chessboard[row - 86][col - 86] == (char) 43) {
                        chessboard[row - 86][col - 86] = (char) 38;
                        this.judge();
                        this.show();
                    } else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }
                    //如果下棋位置超出棋盘范围，就会提示位置不合理并且由该方选手重新选择下棋位置
                }
                else{
                    System.out.println("位置不合理哦！请重新下棋并把把棋子放在棋盘里面！");
                    continue;
                }
                //swap为false时，白方棋手下棋，与黑方选手下棋实现原理相同
            }
            else{
                System.out.println("下面由白方下棋！");
                System.out.println("请输入白棋下落的坐标，横竖坐标只限一位，如（0 0）：");
                row = sc.next().charAt(0);
                col = sc.next().charAt(0);
                //第一部分：横纵坐标都是数字的时候
                if(row <= 57 && row >= 48 && col <= 57 && col >= 48){
                    if(chessboard[row - 47][col - 47] == (char) 43) {
                        chessboard[row - 47][col - 47] = (char) 35;
                        this.judge();
                        this.show();
                    }else{
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }
                }

                else if(row <= 102 && row >= 97 && col <= 57 && col >= 48) {
                    if (chessboard[row - 86][col - 47] == (char) 43) {
                        chessboard[row - 86][col - 47] = (char) 35;
                        this.judge();
                        this.show();
                    } else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }
                }

                else if(row <= 57 && row >= 48 && col <= 102 && col >= 97){
                    if (chessboard[row - 47][col - 86] == (char) 43) {
                        chessboard[row - 47][col - 86] = (char) 35;
                        this.judge();
                        this.show();
                    } else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }
                }

                else if(row <= 102 && row >= 97 && col <= 102 && col >= 97){
                    if (chessboard[row - 86][col - 86] == (char) 43) {
                        chessboard[row - 86][col - 86] = (char) 35;
                        this.judge();
                        this.show();
                    } else {
                        System.out.println("该位置已经有棋子咯，请重新下棋！");
                        continue;
                    }
                }
                else{
                    System.out.println("位置不合理哦！请重新下棋并把把棋子放在棋盘里面！");
                    continue;
                }
            }
            swap = !swap;                                                                       //黑白双方轮流下棋
        }
    }
    public void judge() {
        outer:
        for (int i = 0; i < chessboard.length; i++) {
            for (int j = 0; j < chessboard[i].length; j++) {
                //依然是分成四个部分判断输赢

                //第一部分：可以横向并且往右判断有无连续五个相同棋子
                if (j <= 12){
                    if(chessboard[i][j] == (char) 42 && chessboard[i][j + 1] == (char) 42 && chessboard[i][j + 2] == (char) 42 && chessboard[i][j + 3] == (char) 42 && chessboard[i][j + 4] == (char) 42){
                        System.out.println("恭喜黑方获胜！");
                        flag = false;
                        break outer;
                    }
                    if(chessboard[i][j] == (char) 35 && chessboard[i][j + 1] == (char) 35 && chessboard[i][j + 2] == (char) 35 && chessboard[i][j + 3] == (char) 35 && chessboard[i][j + 4] == (char) 35){
                        System.out.println("恭喜白方获胜！");
                        flag = false;
                        break outer;
                    }
                }
                //第二部分：可以纵向并且往下判断有无连续五个相同棋子
                if(i <= 12){
                    if(chessboard[i][j] == (char) 42 && chessboard[i + 1][j] == (char) 42 && chessboard[i + 2][j] == (char) 42 && chessboard[i + 3][j] == (char) 42 && chessboard[i + 4][j] == (char) 42) {
                        System.out.println("恭喜黑方获胜！");
                        flag = false;
                        break outer;
                    }
                    if(chessboard[i][j] == (char) 35 && chessboard[i + 1][j] == (char) 35 && chessboard[i + 2][j] == (char) 35 && chessboard[i + 3][j] == (char) 35 && chessboard[i + 4][j] == (char) 35){
                        System.out.println("恭喜白方获胜！");
                        flag = false;
                        break outer;
                    }
                }
                //第三部分：可以往右下方判断有无连续五个相同棋子
                if(i <= 12 && j <= 12){
                    if(chessboard[i][j] == (char) 42 && chessboard[i + 1][j + 1] == (char) 42 && chessboard[i + 2][j + 2] == (char) 42 && chessboard[i + 3][j + 3] == (char) 42 && chessboard[i + 4][j + 4] == (char) 42) {
                        System.out.println("恭喜黑方获胜！");
                        flag = false;
                        break outer;
                    }
                    if(chessboard[i][j] == (char) 35 && chessboard[i + 1][j + 1] == (char) 35 && chessboard[i + 2][j + 2] == (char) 35 && chessboard[i + 3][j + 3] == (char) 35 && chessboard[i + 4][j + 4] == (char) 35) {
                        System.out.println("恭喜白方获胜！");
                        flag = false;
                        break outer;
                    }
                }
                //第四部分：可以往右上方判断有无连续五个相同棋子
                if(i >= 5 && j <= 12) {
                    if (chessboard[i][j] == (char) 42 && chessboard[i - 1][j + 1] == (char) 42 && chessboard[i - 2][j + 2] == (char) 42 && chessboard[i - 3][j + 3] == (char) 42 && chessboard[i - 4][j + 4] == (char) 42) {
                        System.out.println("恭喜黑方获胜！");
                        flag = false;
                        break outer;
                    }
                    if (chessboard[i][j] == (char) 35 && chessboard[i - 1][j + 1] == (char) 35 && chessboard[i - 2][j + 2] == (char) 35 && chessboard[i - 3][j + 3] == (char) 35 && chessboard[i - 4][j + 4] == (char) 35) {
                        System.out.println("恭喜白方获胜！");
                        flag = false;
                        break outer;
                    }
                }
            }
        }
    }
}